/*
 * NewDegreeSequenceGraphPanel.java
 * Created on Jul 20, 2010, 3:32:51 PM
 */

package graphexplorer.dialogs;

import org.bm.blaise.scio.graph.DegreeRandomGraph;
import org.bm.blaise.scio.graph.Graph;

/**
 *
 * @author elisha
 */
public class NewDegreeSequenceGraphPanel extends javax.swing.JPanel {

    /** Creates new form NewRandomGraphPanel */
    public NewDegreeSequenceGraphPanel() {
        initComponents();
    }

    /** @return graph corresponding to the current graph settings */
    public Graph<Integer> getInstance() {
        int[] degs = getDegreeSequence();
        if (degs == null)
            return null;
        return DegreeRandomGraph.getInstance(degs, getDirected());
    }

    /** @return order specified by this panel */
    public int[] getDegreeSequence() {
        String text = degreeSeqTF.getText();
        String[] responseArr = text.split(",");
        int[] result = new int[responseArr.length];
        try {
            for (int i = 0; i < responseArr.length; i++)
                result[i] = Integer.decode(responseArr[i].trim());
        } catch (NumberFormatException ex) { 
            return null;
        }
        return result;
    }

    /** @return directed property specified by this panel */
    public boolean getDirected() { return directedRB.isSelected(); }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        edgeTypeBG = new javax.swing.ButtonGroup();
        directedBG = new javax.swing.ButtonGroup();
        undirectedRB = new javax.swing.JRadioButton();
        directedRB = new javax.swing.JRadioButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        degreeSeqTF = new javax.swing.JTextField();

        setBorder(javax.swing.BorderFactory.createTitledBorder("Basic Graph Properties"));
        setLayout(new javax.swing.BoxLayout(this, javax.swing.BoxLayout.LINE_AXIS));

        directedBG.add(undirectedRB);
        undirectedRB.setSelected(true);
        undirectedRB.setText("undirected");
        add(undirectedRB);

        directedBG.add(directedRB);
        directedRB.setText("directed ");
        add(directedRB);

        jSeparator1.setOrientation(javax.swing.SwingConstants.VERTICAL);
        jSeparator1.setMaximumSize(new java.awt.Dimension(2, 32767));
        add(jSeparator1);

        jLabel1.setText("  Degree sequence: ");
        add(jLabel1);

        degreeSeqTF.setText("0,3,2,3");
        degreeSeqTF.setToolTipText("<html>\nEnter sequence of degrees, starting with nodes of degree 0.<br>\nFor example, the sequence <i>0,3,2,3</i> indicates a graph<br>\nwith 3 nodes of degree 1, 2 of degree 2, and 3 of degree 3<br>\n(for a total of 8 nods).");
        degreeSeqTF.setPreferredSize(new java.awt.Dimension(140, 22));
        add(degreeSeqTF);
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField degreeSeqTF;
    private javax.swing.ButtonGroup directedBG;
    private javax.swing.JRadioButton directedRB;
    private javax.swing.ButtonGroup edgeTypeBG;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JRadioButton undirectedRB;
    // End of variables declaration//GEN-END:variables

}
